
package com.app.tablet.view;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.app.tablet.R;

public class ScheduleListActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	boolean[] isselected;
	List<String> schedule_1;
	List<String> schedule_2;
	ListView list_Schedule;
	private Button leftBtn;
	private Button rightBtn;
	private TextView titletxt;
	ScheduleAdapter arrayAdap=null; 
	private int selectedPosition = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_schedulelistpage);
		setHeaderContent();
		
		list_Schedule = (ListView) findViewById(R.id.list_schedule);
		list_Schedule.setOnItemClickListener(this);
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);

		Resources res = getResources();
		schedule_1 = Arrays.asList(res.getStringArray(R.array.tablet_schedule_first_items));
		schedule_2 = Arrays.asList(res.getStringArray(R.array.tablet_schedule_second_items));
		isselected = new boolean[schedule_1.size()];
		
		arrayAdap = new ScheduleAdapter(this, 0, schedule_1);
		list_Schedule.setAdapter(arrayAdap);
		
	}

	private void setHeaderContent() {
		leftBtn = (Button) findViewById(R.id.leftHeaderBtn);
		leftBtn.setText(R.string.cancelBtn);

		rightBtn = (Button) findViewById(R.id.rightHeaderBtn);
		rightBtn.setText(R.string.doneBtn);
		
		titletxt =(TextView)findViewById(R.id.titleHeaderTxt);
		titletxt.setText(R.string.titleSchedulelistpageTxt);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
			this.finish();
			break;
		case R.id.rightHeaderBtn:
			if (selectedPosition != -1) {
				AddPageActivity.dbData.setSchedule(schedule_1.get(selectedPosition));
				this.finish();
			}else{
				Toast.makeText(getApplicationContext(), "Choose a schedule.", 500).show();
			}
			break;
		}
	}

	class ScheduleAdapter extends ArrayAdapter<String> {

		Context adcontext;

		public ScheduleAdapter(Context context, int textViewResourceId, List<String> objects) {
			super(context, textViewResourceId, objects);
			adcontext = context;
		}

		@Override
		public int getCount() {
			return schedule_1.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) adcontext.getSystemService(adcontext.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.layout_listitem_schedulelist, null);
			TextView txtname = (TextView) rowview.findViewById(R.id.txt_schedule_line2);
			TextView txtname_2 = (TextView) rowview.findViewById(R.id.txt_schedule_line1);
			ImageView imgvw1 = (ImageView)rowview.findViewById(R.id.img_tick_schedule);
			txtname.setText("("+schedule_1.get(position)+")");
			txtname_2.setText(schedule_2.get(position));
			if(isselected[position]){
				imgvw1.setVisibility(View.VISIBLE);
			}else{
				imgvw1.setVisibility(View.INVISIBLE);
			}
			return rowview;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		for(int index=0; index<isselected.length;index++){
			isselected[index]=false;
		}
		isselected[position]=true;
		selectedPosition = position;
		refreshListView();
	}

	private void refreshListView() {
		list_Schedule.invalidate();
		arrayAdap.notifyDataSetChanged();
	}

}
